#pragma once

// MySQL版本
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
#include <unordered_map>
#include <fstream>

#include "../comm/log.hpp"
#include"../comm/Util.hpp"
#include "include/mysql.h"

using namespace ns_log;

namespace ns_model
{
    using namespace std;
    struct Question
    {
    public:
        string number; // 题目编号
        string title;  // 题目标题
        string star;   // 题目难度
        string desc;   // 题目描述
        string header; // 题目中可以编写代码
        string tail;   // 题目的测试用例
        int cpu_lim;   // 题目时间限制（s）
        int mem_lim;   // 题目空间限制（kb）
    };
    const std::string oj_questions = "questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "159753";    
    const std::string db = "oj";
    const int port = 3306;
    class Model
    {
    public:
        Model()
        {
        }
        bool QueryMySql(const string &sql, vector<Question > *out)
        {
            //创建mysql句柄
            MYSQL my;
            mysql_init(&my);
           
            //连接数据库
            if(nullptr == mysql_real_connect(&my, host.c_str(), user.c_str(), passwd.c_str(),db.c_str(),port, nullptr, 0))
            {
                LOG(FATAL)<<"连接数据库失败"<<"\n";
               cout<<mysql_error(&my)<<endl; 
                return false;
            }

            //设置编码格式
            mysql_set_character_set(&my,"utf8");

            LOG(DEBUG)<<"连接数据库成功"<<"\n";
            if(mysql_query(&my,sql.c_str())!=0)
            {
                LOG(WARNING) << sql << " execute error!" << "\n";
                return false;
            }

            //提取结果
            MYSQL_RES *res=mysql_store_result(&my);

            //分析结果
            int rows=mysql_num_rows(res);
            int cols=mysql_num_fields(res);
            Question q;

            for(int i=0;i<rows;i++)
            {
                MYSQL_ROW row=mysql_fetch_row(res);
               q.number=row[0];
               q.title=row[1];
               q.star=row[2];
               q.desc=row[3];
               q.header=row[4];
               q.tail=row[5];
               q.cpu_lim=atoi(row[6]);
               q.mem_lim=atoi(row[7]);

               out->push_back(q);
            }
            //释放空间
            free(res);
            mysql_close(&my);
            return true;
        }
        // 获取题目信息
        bool GetAllQuestions(vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql, out);
        }
        bool GetOneQuest(string &quest_num, Question *out)
        {
            bool res = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where id=";
            sql += quest_num;
            vector<Question> result;
            if(QueryMySql(sql, &result))
            {
                if(result.size() == 1){
                    *out = result[0];
                    res = true;
                }
            }
            return res;
        }
        ~Model()
        {
        }

    private:
    };
}